import { app, BrowserWindow, ipcMain, Menu, Tray } from 'electron'
const ipc = ipcMain;
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

// 初始化用户目录结构
const fs = require("fs");
function createUserDir() {
  fs.exists("data/", (exists) => {
    if (!exists) {
      fs.mkdir("data/", err => {
        if (!err) {
          fs.mkdir("data/.shadow/", err => {
            if (!err) {
              fs.mkdir("data/.shadow/pub/", err => { });
            }
          });
        }
      });
    }
  });
  fs.exists("storage/", (exists) => {
    if (!exists) {
      fs.mkdir("storage/", err => {});
    }
  })
}

let mainWindow;
let tray = null;

const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createLoginWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true
    },
    useContentSize: true,
    height: 220,
    width: 430,
    resizable: false,
    movable: true,
    center: true,
    minimizable: false,
    frame: false,
    transparent: true,
    alwaysOnTop: false
  })

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', () => {
  createUserDir();
  createLoginWindow();
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (loginWindow === null) {
    createLoginWindow()
  }
})

ipc.on('create-tray', event => {
  tray = new Tray("build/icons/icon.ico");
  const contextMenu = Menu.buildFromTemplate([
    {
      label: '设置', click: function () {
      }
    },
    {
      label: '帮助', click: function () {
      }
    },
    {
      label: '关于', click: function () {
      }
    },
    { type: 'separator' },
    {
      label: '退出', click: function () {
        app.quit();
        app.quit();
      }
    }
  ]);
  tray.setToolTip("smssc");
  tray.setContextMenu(contextMenu);
  tray.on("click", () => {
    if (!mainWindow.isVisible()) {
      mainWindow.show();
      mainWindow.setSkipTaskbar(false);
    }
  })
});

ipc.on('hide', event => {
  if (mainWindow.isVisible()) {
    mainWindow.hide();
    mainWindow.setSkipTaskbar(true);
  }
});

ipc.on('quit', event => {
  if (mainWindow !== null) {
    mainWindow.close();
  }
})

ipc.on('restart', event => {
  if (mainWindow !== null) {
    if (tray !== null) {
      tray.destroy();
      tray = null;
    }
    mainWindow.close();
    createLoginWindow();
  }
});


/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
